<?php

namespace system\libs\validator;

/**
 * Representation of field in {@link Validator} context.
 *
 * @see Validator
 * @package system\libs\validator
 * @author Pavel Egorov {@link mailto:pavel@egorov.ru pavel@egorov.ru}
 * @version $Revision: 63 $
 * @copyright Copyright (c) 2011, Pavel Egorov
 */
class FormField {

    private $fieldValue;
    private $fieldName;
    private $err;
    private $errorTriger = TRUE;

    public function __construct($fieldName, $fieldValue) {
        $this->fieldValue = $fieldValue;
        $this->fieldName = $fieldName;
    }

    public function notNull() {
        if ($this->errorTriger && $this->fieldValue == NULL) {
            $this->err[$this->fieldName]['NotNull'] = 'fail';
            $this->errorTriger = FALSE;
        }
        return $this;
    }

    public function exactLenght($len) {
        if ($this->errorTriger && strlen($this->fieldValue) != $len) {
            $this->err[$this->fieldName]['ExactLenght'] = 'fail';
            $this->errorTriger = FALSE;
        }
        return $this;
    }

    public function maxLenght($max) {
        if ($this->errorTriger && strlen($this->fieldValue) > $max) {
            $this->err[$this->fieldName]['MaxLenght'] = 'fail';
            $this->errorTriger = FALSE;
        }
        return $this;
    }

    public function minLenght($min) {
        if ($this->errorTriger && strlen($this->fieldValue) < $min) {
            $this->err[$this->fieldName]['MinLenght'] = 'fail';
            $this->errorTriger = FALSE;
        }
        return $this;
    }

    public function valid_email() {
        if ($this->errorTriger && !preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $this->fieldValue)) {
            $this->err[$this->fieldName]['Email'] = 'fail';
            $this->errorTriger = FALSE;
        }
        return $this;
    }

    public function alfaNumeric() {
        if ($this->errorTriger && !preg_match("/^([A-Za-zа-я _-А-Я0-9])+$/i", $this->fieldValue)) {
            $this->err[$this->fieldName]['AlfaNumeric'] = 'fail';
            $this->errorTriger = FALSE;
        }
        return $this;
    }

    public function alfa() {
        if ($this->errorTriger && !preg_match("/^([A-Za-zа-я _-А-Я])+$/ui", $this->fieldValue)) {
            $this->err[$this->fieldName]['Alfa'] = 'fail';
            $this->errorTriger = FALSE;
        }
        return $this;
    }

    public function numeric() {
        if ($this->errorTriger && !preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $this->fieldValue)) {
            $this->err[$this->fieldName]['Numeric'] = 'fail';
            $this->errorTriger = FALSE;
        }
        return $this;
    }

    public function compareTo($obj) {
        if ($this->errorTriger && $this->fieldValue != $obj) {
            $this->err[$this->fieldName]['CompareTo'] = 'fail';
            $this->errorTriger = FALSE;
        }
        return $this;
    }

    public function isValid() {
        return $this->errorTriger;
    }

    public function getErrors() {
        return $this->err;
    }

}